<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
	'id'=>'gl-form',
	'enableAjaxValidation'=>false,
)); ?>

<p class="help-block">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>
<?php if($this->inputMode == 'edit') { ?>	
	<div class="form-group ">
		<label class="col-sm-3 control-label required" for="Gl_glType">Parent Ledger</label>
		<div class="col-sm-9 form-control-value" ><strong><?php echo Gl::model()->findByPk($model->parentUid)->title; ?></strong></div>
	</div>
	<div class="form-group ">
		<label class="col-sm-3 control-label required" for="Gl_glType">Gl Type</label>
		<div class="col-sm-9 form-control-value" ><strong><?php echo GlType::model()->findByPk($model->glType)->title; ?></strong></div>
	</div>
	<div class="form-group ">
		<label class="col-sm-3 control-label required" for="Gl_glType">Ledger Id</label>
		<div class="col-sm-9 form-control-value"><strong><?php echo $model->codePrefix, '-', $model->codePostfix; ?></strong></div>
	</div>	
	<?php echo $form->textFieldRow($model,'title',array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>200)); ?>
	<?php echo $form->textFieldRow($model,'telephone',array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>50)); ?>	
<?php } else { ?>
	<?php echo $form->dropDownListRow($model,'parentUid',CHtml::listData(Gl::getDropdownList('treeLevel < 2'), 'id', 'title'), array('empty'=>'','labelOptions'=>array('label'=>'Parent Ledger', 'class'=>'col-sm-3'),'class'=>'form-control')); ?>
	<?php echo $form->dropDownListRow($model,'glType',CHtml::listData(Gltype::model()->findAll(), 'id', 'title'), array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control')); ?>
	<?php echo $form->textFieldRow($model,'codePrefix',array('labelOptions'=>array('label'=>'Code Prefix', 'class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>2)); ?>
	<?php echo $form->textFieldRow($model,'codePostfix',array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>6)); ?>
	<?php echo $form->textFieldRow($model,'title',array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>200)); ?>
	<?php echo $form->textFieldRow($model,'telephone',array('labelOptions'=>array('class'=>'col-sm-3'),'class'=>'form-control','maxlength'=>50)); ?>
<?php } ?>

<div class="form-actions">
	<?php $this->widget('bootstrap.widgets.TbButton', array(
		'buttonType'=>'submit',
		'type'=>'primary',
		'label'=>$model->isNewRecord ? 'Create' : 'Save',
	)); ?>
</div>
<?php $this->endWidget(); ?>

<script>
	$('#<?php echo $form->id, ' #Gl_parentUid'; ?>').change(function(){
		if(this.value == ''){
			$('#<?php echo $form->id;?> #Gl_codePrefix').val('').attr('readonly',false);
			$('#<?php echo $form->id;?> #Gl_codePostfix').val('').attr('readonly',false);;
			$('#<?php echo $form->id;?> #Gl_glType').val(1).attr('disabled',false);;
			return;
		}
		data = {id:this.value};
		$.ajax({
			url: '<?php echo Yii::app()->urlManager->createUrl('gl/jsonview'); ?>',
			type: 'post',
			data: data,
			dataType: 'json',
			success: function(gl) {
				$('#<?php echo $form->id;?> #Gl_codePrefix').val(gl.codePrefix).attr('readonly','readonly');
				$('#<?php echo $form->id;?> #Gl_codePostfix').val('auto').attr('readonly','readonly');
				$('#<?php echo $form->id;?> #Gl_glType').val(gl.glType).attr('disabled','true');
			},
			error: function() {
				alert('An error has occured while adding a new block.');
			}
		});
	});
	<?php if($this->inputMode == 'edit') { ?>	
	$(document).ready(function(){
		('<?php echo $form->id, ' #Gl_parentUid'; ?>').trigger("change");
	});
	<?php } ?>	
</script>
